/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/23
 * Time: 13:37
 */
$(function(){
    $('.btn-save').on('click', function(){
        var params = {
            username: $('.username').val(),
            email: $('.email').val()
        };
        if (checkForm(params)) {
            $.ajax({
                type: 'post',
                url: '/sendEmail',
                data: {params: params},
                success: function(e){
                    var data = JSON.parse(e);
                    if (data.status == 200) {
                        alert('新密码已发送至您的邮箱,请及时查收!');
                        location.href = '/login.html';
                    } else {
                        alert('用户名或电子邮箱错误!');
                    }
                }
            });
        }
    });
});

function checkForm(params) {
    if (params.username == '') {
        alert('请输入用户名');
        return false;
    }
    if (params.email == '') {
        alert('请输入电子邮箱');
        return false;
    }
    var emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
    if (!emailReg.test($.trim(params.email))) {
        alert('电子邮箱格式不正确');
        return false;
    }
    return true;
}